/**
 *  编辑页
 *
 * 作者 admin
 * 日期 2024-08-20 15:49:12
 */
layui.use(['jquery','febs', 'form', 'validate','fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        commonJS = layui.commonJS,
        $view = $('#hazardExamineBaseInfoDetail-edit'),
        tableIns,
        tableIns_company,
        validate = layui.validate;
    let groupUserTableData_company = [];

    form.verify(validate);
    form.render();
    initGroupUserTable_company();
    initFormValue();

    function initGroupUserTable_company() {
        tableIns_company = febs.table.init({
            elem: $view.find('#groupUserTable_company'),
            id: 'groupUserTable_company',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'examineTeamId', title: 'id',hide:true},
                {field: 'teamName',title: '分组名称', minWidth: 220},
                {field: 'teamUserList', title: '检查人员', minWidth: 500, templet: function (d) {
                        var flg = '';
                        var users = d.teamUserList;
                        if(users){
                            users.forEach((item, index)=> {
                                flg += '<span class="layui-badge febs-bg-orange" style="margin-left:5px; ">'+item.realName+'</span>'
                            })
                        }
                        return flg;
                    }
                },
            ]],
            done: function(res, curr, count){
            },
            data:groupUserTableData_company
        });
    }
    function initFormValue() {
        if (hazardExamineData) {
            form.val("hazardExamine-edit-form", {
                "examineId": hazardExamineData.examineId,
                "tenantId": hazardExamineData.tenantId,
                "examineNo": hazardExamineData.examineNo,
                "examineCategory": hazardExamineData.examineCategory,
                "examineType": hazardExamineData.examineType,
                "examineTitle": hazardExamineData.examineTitle,
                "startDate": hazardExamineData.startDate,
                "endDate": hazardExamineData.endDate,
                "objectId": hazardExamineData.objectId,
                "objectName": hazardExamineData.objectName,
                "remark": hazardExamineData.remark,
                "status": hazardExamineData.status,
            });

            //加载检查项
            if(hazardExamineData.objectList && hazardExamineData.objectList.length>0){
                //检查类型
                let examineType = hazardExamineData.examineType;
                let objectList = hazardExamineData.objectList;
                for(let i=0;i<objectList.length;i++){
                    appendHtml(i,objectList[i],examineType);
                }
            }

        }
    }

    function appendHtml(sort,item,examineType){
        let teamList = item.teamList;
        if(examineType =='1'){
            //公司检查
            $view.find("#company_field").show();
            if(teamList && teamList.length>0){
                reloadTab_company(teamList);
            }
        }else if(examineType =='5'){
            //项目检查
            $view.find("#project_field").show();
            //生成元素
            appendHtml_project(sort);
            //渲染数据
            renderData_project(sort,item);
        }else{
            //部门、车间、班组检查
            $view.find("#dept_field").show();
            //生成元素
            appendHtml_dept(sort,examineType);
            //渲染数据
            renderData_dept(sort,item);
        }
    }

    function reloadTab_company(data){
        tableIns_company.reload({data:data});
    }

    //拼接检查项（项目）
    function appendHtml_project(sort){
        var groupPanel_project = '<div class="layui-panel layui-panel-project" id="panel_project' + sort + '">' +
            '                        <div class="layui-field-box">' +
            '                            <span class="layui-badge" >'+(sort+1)+'</span>' +
            '                            <div class="layui-form-item" >' +
            '                                <div class="layui-row">' +
            '                                    <div class="layui-col-xs6">' +
            '                                        <label class="layui-form-label layui-form-label-detail">项目名称：</label>' +
            '                                        <div class="layui-input-block layui-input-block-html" id="projectSelect' + sort + '">' +
            '                                        </div>' +
            '                                        </div>' +
            '                                </div>' +
            '                            </div>' +
            '                            <div class="layui-form-item">' +
            '                                <div class="layui-row">' +
            '                                    <label class="layui-form-label layui-form-label-detail">检查人员：</label>' +
            '                                    <div class="layui-input-block">' +
            '                                        <table lay-filter="groupUserTable_project" id="groupUserTable_project' + sort + '" lay-data="{id: \'groupUserTable_project\'' + sort + '}"></table>' +
            '                                    </div>' +
            '                                </div>' +
            '                            </div>' +
            '                        </div>' +
            '                    </div>';
        $("#project_box").append(groupPanel_project);
        //分组人员table
        initGroupUserTable($view.find('#groupUserTable_project'+sort),'groupUserTable_project'+sort,[]);
        form.render();
    }

    function renderData_project(sort,item){
        //项目下拉框
        $("#projectSelect"+sort).text(item.objectName);
        //分组人员table
        let teamList = item.teamList;
        if(teamList && teamList.length>0){
            initGroupUserTable($view.find('#groupUserTable_project'+sort),'groupUserTable_project'+sort,teamList);
        }
    }

    //拼接检查项（部门、车间、班组检查）
    function appendHtml_dept(sort,examineType){
        var label = '';
        if(examineType =='2'){
            label = '部门名称';
        }else if(examineType =='3'){
            label = '车间名称';
        }else if(examineType =='4'){
            label = '班组名称';
        }
        var groupPanel_dept = '<div class="layui-panel layui-panel-dept" id="panel_dept' + sort + '">' +
            '                        <div class="layui-field-box">' +
            '                            <span class="layui-badge" >'+(sort+1)+'</span>' +
            '                            <div class="layui-form-item" >' +
            '                                <div class="layui-row">' +
            '                                    <div class="layui-col-xs6">' +
            '                                        <label class="layui-form-label layui-form-label-detail" id="deptNameLabel">'+label+'：</label>' +
            '                                        <div class="layui-input-block layui-input-block-html" id="deptId_deptExamine' + sort + '">' +
            '                                        </div>' +
            '                                    </div>' +
            '                                </div>' +
            '                            </div>' +
            '                            <div class="layui-form-item">' +
            '                                <div class="layui-row">' +
            '                                    <label class="layui-form-label layui-form-label-detail">检查人员：</label>' +
            '                                    <div class="layui-input-block">' +
            '                                        <table lay-filter="groupUserTable_dept" id="groupUserTable_dept' + sort + '" lay-data="{id: \'groupUserTable_dept\'' + sort + '}"></table>' +
            '                                    </div>' +
            '                                </div>' +
            '                            </div>' +
            '                        </div>' +
            '                    </div>';
        $("#dept_box").append(groupPanel_dept);
        //分组人员table
        initGroupUserTable($view.find('#groupUserTable_dept'+sort),'groupUserTable_dept'+sort,[]);
        form.render();
    }

    function renderData_dept(sort,item){
        //部门下拉框
        $("#deptId_deptExamine"+sort).text(item.objectName);
        //分组人员table
        let teamList = item.teamList;
        if(teamList && teamList.length>0){
            initGroupUserTable($view.find('#groupUserTable_dept'+sort),'groupUserTable_dept'+sort,teamList);
        }
    }

    function initGroupUserTable(elem,tableId,tableData) {
        febs.table.init({
            elem: elem,
            id: tableId,
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'examineTeamId', title: 'id',hide:true},
                {field: 'teamName',title: '分组名称', width: 220},
                {field: 'teamUserList', title: '检查人员', minWidth: 500, templet: function (d) {
                        var flg = '';
                        var users = d.teamUserList;
                        if(users){
                            users.forEach((item, index)=> {
                                flg += '<span class="layui-badge febs-bg-orange" style="margin-left:5px; ">'+item.realName+'</span>'
                            })
                        }
                        return flg;
                    }
                },
            ]],
            done: function(res, curr, count){
            },
            data:tableData
        });
    }

});